# Copyright (c) 2009 Olexandr Melnyk <me@omelnyk.net>
# See COPYING for details.

import re
import urllib2

# Translation interface for dict.leo.org
class LeoDictionary:
    # Constructor
    def __init__(self):
        self.short_name = "leo"
        self._word_url = 'http://dict.leo.org/ende?search=%s'

    # Returns list of supported language pairs
    def supported_pairs(self):
        return [("en", "de"), ("de", "en")]
    
    # Translates word from source_lang to target_lang
    def translate(self, word, source_lang, target_lang):
        html    = urllib2.urlopen(self._word_url % word).read()
        pattern = '(<td[^>]*>)([^<]+)(<\/td>)(<td valign="middle" width="43%">)(<A[^>]*>)([^<]+)(<\/A>)([^<]*)(<A[^>]*>)([^<]+)(<\/A>)(.*?)(<\/td>)'
        results = re.findall(pattern, html)
        
        return [res[5] + ' ' + res[9] for res in results]
